package com.kushe.dao;

import com.kushe.entity.Sort;
import com.kushe.util.cache.CacheKit;
import com.kushe.util.cache.UseCache;
import com.kushe.util.core.DataAccess;
import org.nutz.dao.Cnd;

import java.util.List;

/**
 * Created by WhiteBlue on 15/5/2.
 */
public class SortDao extends DataAccess{

    public Sort get(int id) {
        return getDao().fetch(Sort.class, id);
    }

    @UseCache(cacheName = "sortList")
    public List<Sort> listAll() {
        return getDao().query(Sort.class, Cnd.orderBy().desc("name"));
    }

    @UseCache(cacheName = "sortIndex")
    public List<Sort> listIndex() {
        return getDao().query(Sort.class, Cnd.limit().limit(1,5).desc("pv"));
    }

    public void addPV(Sort sort) {
        if (CacheKit.get("pv", 0) == null) {
            CacheKit.put("pv", 0, 0);
        } else {
            int pv = (Integer) CacheKit.get("pv", 0);
            pv++;
            if (pv > 20) {
                sort.setPv(sort.getPv() + pv);
                getDao().update(sort);
            }
            CacheKit.put("pv", 0, pv);
        }

    }

}
